package kr.pe.johnkim.carrotpoint.mobile.dao;

import kr.pe.johnkim.carrotpoint.mobile.domain.CarrotPointMember;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

/**
 * Carrot Point 사용자 관련 dao 클래스 이다.
 *
 * @author : johnkim <cale9797@gmail.com>
 * @date: 13. 1. 31.
 */
@Repository
public class CarrotPointMemberDaoImpl extends SqlSessionDaoSupport implements CarrotPointMemberDao {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(CarrotPointMemberDaoImpl.class);

    /**
     * 최초 가입시 사용자 아이디로 중복 검사를 한다.
     *
     * @param carrotPointMember
     * @return
     */
    @Override
    public CarrotPointMember getMemberExistById(CarrotPointMember carrotPointMember) {
        logger.info("getMemberExistById");
        return (CarrotPointMember)getSqlSession().selectOne("CARROTPOINT_MEMBER.getMemberExistById", carrotPointMember);
    }

    /**
     * 사용자를 등록 한다.
     *
     * @param carrotPointMember
     */
    @Override
    public void registerMember(CarrotPointMember carrotPointMember) {
        logger.info("registerMember");
        this.getSqlSession().insert("CARROTPOINT_MEMBER.registerMember", carrotPointMember);
    }

    /**
     * 사용자 정보를 가져 온다.
     *
     * @param carrotPointMember
     * @return
     */
    @Override
    public CarrotPointMember getMemberInformation(CarrotPointMember carrotPointMember) {
        logger.info("getMemberInformation");

        logger.info("memberId   : {}", carrotPointMember.getMemberId());
        logger.info("activateYn : {}", carrotPointMember.getActivateYn());

        return (CarrotPointMember)getSqlSession().selectOne("CARROTPOINT_MEMBER.getMemberInformation", carrotPointMember);
    }

    /**
     * 사용자 정보를 업데이트 한다.
     *
     * @param carrotPointMember
     */
    @Override
    public void updateMemberInformation(CarrotPointMember carrotPointMember) {
        logger.info("updateMemberInformation");
        this.getSqlSession().update("CARROTPOINT_MEMBER.updateMemberInformation", carrotPointMember);
    }

    /**
     * 사용자의 GCM Registration Id / APNS Device Token 을 업데이트 한다.
     *
     * @param carrotPointMember
     */
    @Override
    public void updateRegistrationId(CarrotPointMember carrotPointMember) {
        logger.info("updateRegistrationId");

        logger.info("registration id : {}", carrotPointMember.getRegistrationId());

        this.getSqlSession().update("CARROTPOINT_MEMBER.updateRegistrationId", carrotPointMember);
    }

    /**
     * Push Notification 수신 여부를 업데이트 한다.
     *
     * @param carrotPointMember
     */
    @Override
    public void updateReceiveNotificationYn(CarrotPointMember carrotPointMember) {
        logger.info("updateReceiveNotificationYn");
        this.getSqlSession().update("CARROTPOINT_MEMBER.updateReceiveNotificationYn", carrotPointMember);
    }

    /**
     * 사용자의 위치 정보를 업데이트 한다.
     *
     * @param carrotPointMember
     */
    @Override
    public void updateMemberLocation(CarrotPointMember carrotPointMember) {
        logger.info("updateMemberLocation");

        logger.info("latitude  : {}", carrotPointMember.getLatitude());
        logger.info("longitude : {}", carrotPointMember.getLongitude());

        this.getSqlSession().update("CARROTPOINT_MEMBER.updateMemberLocation", carrotPointMember);
    }

    /**
     * 사용자의 활성화 여부를 업데이트 한다.
     *
     * @param carrotPointMember
     */
    @Override
    public void updateMemberActivateYn(CarrotPointMember carrotPointMember) {
        logger.info("updateMemberActivateYn");
        this.getSqlSession().update("CARROTPOINT_MEMBER.updateMemberActivateYn", carrotPointMember);
    }

    /**
     * 전화번호로 기존 사용자가 있는지 확인 한다.
     *
     * @param carrotPointMember
     * @return
     */
    @Override
    public CarrotPointMember getMemberExistByPhoneNumber(CarrotPointMember carrotPointMember) {
        logger.info("getMemberExistByPhoneNumber");

        logger.info("phone number : {}", carrotPointMember.getPhoneNumber());

        return (CarrotPointMember)getSqlSession().selectOne("CARROTPOINT_MEMBER.getMemberExistByPhoneNumber", carrotPointMember);
    }

    /**
     * 전화번호로 기존 사용자 정보를 가져 온다.
     *
     * @param carrotPointMember
     * @return
     */
    @Override
    public CarrotPointMember getMemberInformationByPhoneNumber(CarrotPointMember carrotPointMember) {
        logger.info("getMemberInformationByPhoneNumber");

        logger.info("phone number : {}", carrotPointMember.getPhoneNumber());

        return (CarrotPointMember)getSqlSession().selectOne("CARROTPOINT_MEMBER.getMemberInformationByPhoneNumber", carrotPointMember);
    }
}
